package com.youxin.cpcenter;

import javax.annotation.PostConstruct;

import org.apache.log4j.BasicConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.youxin.busfoundation.common.BuildConfig;
import com.youxin.busfoundation.common.HandleShutdownApp;
import com.youxin.busfoundation.config.EsConfig;
import com.youxin.busfoundation.config.KafkaConfig;
import com.youxin.busfoundation.config.RedisClusterConfig;
import com.youxin.busfoundation.config.WebLoadBalancer;
import com.youxin.busfoundation.config.WxBeanConfig;
import com.youxin.busfoundation.config.ZfbBeanConfig;
import com.youxin.cpcenter.common.dbcache.CacheDataObject;
import com.youxin.cpcenter.config.EmailBeanConfig;
import com.youxin.cpcenter.pt.service.PtTask;
import com.youxin.cpcenter.service.ClassifyService;
import com.youxin.foundation.db.redis.RedisClusterCommand;
import com.youxin.foundation.utils.thread.BaseThreadPool;
import com.youxin.foundation.utils.thread.ScheduleThreadPool;

/**
 * 启动入口
 * 
 * @author yingde.cao
 *
 */
@SpringBootApplication()
@EnableEurekaClient
@RestController
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
@EnableTransactionManagement
@EnableFeignClients
@EnableDiscoveryClient
@EnableCaching
@EnableConfigurationProperties({ KafkaConfig.class, EsConfig.class, EmailBeanConfig.class, WxBeanConfig.class,
		ZfbBeanConfig.class, RedisClusterConfig.class })
// @Configuration
public class CpCenterApplication {
	@Autowired
	private EsConfig esConfig;
	@Autowired
	private KafkaConfig kafkaConfig;
	@Autowired
	private Environment environment;

	@Autowired
	private EmailBeanConfig emailBeanConfig;
	@Autowired
	private WxBeanConfig wxBeanConfig;
	@Autowired
	private ZfbBeanConfig zfbBeanConfig;
	@Autowired
	private RedisClusterConfig redisClusterConfig;

	@Value("${eureka.client.serviceUrl.defaultZone}")
	private String eurekaServer;
	@Value("${spring.datasource.url}")
	private String dataSourceUrl;
	@Autowired
	private LoadBalancerClient loadBalancer;
	@Autowired
	private ClassifyService classifyService;

	// @Autowired
	// private DataSource dataSource;
	//
	 @Autowired
	 private JdbcTemplate jdbcTemplate;

	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(CpCenterApplication.class);

	public static void main(String[] args) {
		 BasicConfigurator.configure(); //防止出现log4j: WARN no
		// appender警告，也可配制解决。
	   // System.setProperty("spring.devtools.restart.enabled", "true");
		//BasicConfigurator.configure();
		SpringApplication.run(CpCenterApplication.class, args);
	}

	@Autowired
	private Environment env;

	@Value("${server.port}")
	String port;

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	/**
	 * 打印当前
	 */
	private void getActiveProfiles() {
		logger.warn("******加载配置,MYSQL:" + dataSourceUrl + ",EurekaServer:" + eurekaServer);
		for (final String profileName : environment.getActiveProfiles()) {
			logger.warn("******当前使用的配置 - " + profileName);
		}
		logger.warn("max Membory:"+ Runtime.getRuntime().maxMemory()+",server.id="+String.valueOf(System.getProperty("server.id")));
		logger.warn("ESHOST:"+String.valueOf(EsConfig.getConfig().getHosts()));
		logger.warn("redis-cluster:"+String.valueOf(redisClusterConfig.getHosts()));
	}

	@PostConstruct
	private void init() {
		try {
			BuildConfig.initWithEnvid(env);
			WebLoadBalancer.initWithBanlancer(loadBalancer);
			KafkaConfig.initConfig(kafkaConfig);
			EsConfig.initConfig(esConfig);
			EmailBeanConfig.initWithConfig(emailBeanConfig);
			
			// kafkaConsumerLog.execute();
			// 初始化 支付宝 微信 参数
			ZfbBeanConfig.initWithConfig(zfbBeanConfig);
			WxBeanConfig.initWithConfig(wxBeanConfig);
			RedisClusterCommand.initConfig(redisClusterConfig);
			
			if (BuildConfig.isTest()) {
				BaseThreadPool.instance(800, Integer.MAX_VALUE, 10000);
				PtTask.instanse().run(jdbcTemplate);
			}else{
				BaseThreadPool.instance(10, Integer.MAX_VALUE, 10000);
				ScheduleThreadPool.instance(6);
			}
			
			getActiveProfiles();

			try {
				HandleShutdownApp.instance().handleSignal(() -> {
				});
			} catch (Exception e) {
				logger.error("加载HandleSignal异常", e);
			}

			CacheDataObject.instance().loadService(classifyService);
		} catch (Exception e) {
			logger.error("加载异常", e);
		}
	}

	// http://localhost:8762/hystrix.stream
	// http://localhost:8762/hystrix/
}
